﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CSW.Framework.Common.HttpUtils;
using System.Net;
using System.Reflection;

namespace CSW.Framework.UI.Forms
{
    /// <summary>
    /// Exception form.
    /// </summary>
    public partial class ExceptionForm : Form
    {
        /// <summary>
        /// Exception form constructor.
        /// </summary>
        /// <param name="message">The error message.</param>
        /// <param name="stackTrace">The stack trace.</param>
        /// <param name="isTopMost">if <c>true</c>, set TopMost = true.</param>
        /// <param name="occuredDuringStartup">set to <c>true</c> if the error occured during application startup.</param>
        public ExceptionForm(string message, string stackTrace, bool isTopMost, bool occuredDuringStartup)
        {
            InitializeComponent();

            if (isTopMost)
            {
                StartPosition = FormStartPosition.CenterScreen;
                TopMost = true;
            }

            if (occuredDuringStartup)
            {
                txtErrorMessage.Text = "This error occurred during startup.\r\n\r\n" + message;
            }
            else
            {
                txtErrorMessage.Text = message;
            }

            txtStackTrace.Text = stackTrace;
        }

        private void cmdExitProgram_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnReportBug_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                SubmitError();

                Cursor.Current = Cursors.Default;

                MessageBox.Show("Thank you.  This bug has been successfully submitted.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                Close();
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Error submitting bug", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// Submits the error.
        /// </summary>
        private void SubmitError()
        {
            List<PostData> postData = new List<PostData>();
            postData.Add(new PostData("dummy", ""));
            postData.Add(new PostData("msg", txtErrorMessage.Text));
            postData.Add(new PostData("stacktrace", txtStackTrace.Text));
            postData.Add(new PostData("os", Environment.OSVersion.ToString()));
            postData.Add(new PostData("ver", Assembly.GetExecutingAssembly().FullName));
            postData.Add(new PostData("email", ""));
            postData.Add(new PostData("comments", ""));

            IWebProxy proxy = WebRequest.DefaultWebProxy;
            proxy.Credentials = new NetworkCredential();

            Http.PostAsync("http://www.cdtag.com/submitbug3.php", postData, proxy);
        }

        private void ExceptionForm_Activated(object sender, EventArgs e)
        {
            txtErrorMessage.Focus();
        }

        private void btnIgnore_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
